<route lang="json5">
{
  style: {
    navigationBarTitleText: '格知汇GACC',
    navigationStyle: 'custom',
  },
}
</route>
<template>
  <view class="live-reservation" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
    <!-- 返回按钮 -->
    <view class="back-btn" @click="goBack">
      <image src="/static/images/icons/icon_return.png" class="back-icon"></image>
    </view>

    <!-- 顶部图片 -->
    <view class="banner-container">
      <image :src="bannerSrc" class="banner-image" @error="handleBannerError"></image>
    </view>
    <view style="position: relative; width: 100%; height: 50rpx">
      <view class="reservation-count">已有{{ reservationCount }}人预约</view>
    </view>

    <!-- 直播信息 -->
    <view class="live-info">
      <view class="live-title">{{ liveInfo.title }}</view>
      <view class="live-time">直播时间：{{ liveInfo.time }}</view>
    </view>

    <!-- 讲师信息 -->
    <view class="teacher-info">
      <view class="teacher-avatar-container">
        <image :src="teacherAvatarSrc" class="teacher-avatar" @error="handleAvatarError"></image>
      </view>
      <view class="teacher-detail">
        <!--        这边不清楚名字的构造，两个选一个吧-->
        <view class="teacher-name">{{ liveInfo.teacherName }}</view>
        <!--        <view class="teacher-name">-->
        <!--          James-->
        <!--          <br />-->
        <!--          Jian Wu-->
        <!--        </view>-->
        <view class="teacher-tag">{{ liveInfo.teacherTag }}</view>
      </view>
      <view class="teacher-field">
        <view class="field-label">
          <image
            src="/static/images/icons/live/icon_star.png"
            style="width: 30rpx; height: 30rpx; margin-right: 10rpx"
          />
          擅长领域:
        </view>
        <view class="field-value">{{ liveInfo.teacherField }}</view>
      </view>
    </view>
    <view class="detail-title">直播详情：</view>
    <!-- 直播详情 -->
    <view class="live-detail">
      <view class="detail-content">
        <rich-text style="margin-bottom: 80rpx; margin-top: 60rpx" :nodes="liveInfo.out" />
      </view>
    </view>
    <view class="reservation-btn" v-if="isReservation" @click="navToLive">进入直播间</view>
    <view class="reservation-btn" v-else @click="makeReservation">立即预约</view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { getCourseLiveDetail, reservationLive, LiveDetail } from '@/service/live/live'

const safeAreaInsets = uni.getSystemInfoSync().safeAreaInsets
// 返回上一页
const goBack = () => {
  uni.navigateBack()
}
const navToLive = () => {
  uni.navigateTo({
    url: '/pages-sub/pages-sub/webview/webviewLive?channelId=' + liveDetail.channelId,
  })
}

// 默认图片
const defaultImage = '/static/images/icons/default.jpg'

// 图片源引用,填写网络请求中的图片源
const bannerSrc = ref('mock')
const teacherAvatarSrc = ref('mock')
const detailImageSrc = ref('mock')

// 图片错误处理函数
const handleBannerError = () => {
  bannerSrc.value = defaultImage
}

const handleAvatarError = () => {
  teacherAvatarSrc.value = defaultImage
}

const handleDetailImageError = () => {
  detailImageSrc.value = defaultImage
}

// 预约人数
const reservationCount = ref(0)
const isReservation = ref(false)
const liveId = ref<number>(0)

// 直播信息 - 按照模板中使用的属性名定义
const liveInfo = reactive({
  title: '',
  time: '',
  teacherName: '',
  teacherTag: '主讲老师',
  teacherField: '',
  description: '',
  out: '', // 用于存储outline内容
  bannerImage: '',
  detailImage: '',
})

// 原始API返回的直播详情
const liveDetail = reactive({} as LiveDetail)

// 预约直播
const makeReservation = () => {
  if (!liveId.value) return

  reservationLive(liveId.value).then((res) => {
    if (res.code === 200) {
      uni.showToast({
        title: '预约成功',
        icon: 'success',
      })
      reservationCount.value++
      isReservation.value = true
    } else {
      uni.showToast({
        title: res.message || '预约失败',
        icon: 'none',
      })
    }
  })
}

// 获取直播详情
const fetchLiveDetail = async (id: number) => {
  try {
    uni.showLoading({ title: '加载中...' })
    const res = await getCourseLiveDetail(id)
    uni.hideLoading()

    if (res.code === 200 && res.data) {
      // 保存原始数据
      Object.assign(liveDetail, res.data)

      // 映射API返回的数据到liveInfo属性
      liveInfo.title = res.data.liveName
      liveInfo.time = `${res.data.startTime} - ${res.data.endTime}`
      liveInfo.teacherName = res.data.teacher?.teacherName || ''
      liveInfo.teacherField = res.data.teacher?.teacherGoodAt || ''
      liveInfo.out =
        res.data.outline.replaceAll('<img ', '<img style="max-width:100%;height:auto"') || ''

      // 更新图片源
      bannerSrc.value = res.data.cover || defaultImage

      // todo 换成正式地址 处理教师头像
      if (res.data.teacher && res.data.teacher.cover) {
        teacherAvatarSrc.value =
          // res.data.teacher.cover.replace('~', 'https://grundfosuat.panzhi.com.cn') || defaultImage
          res.data.teacher.cover.replace(
            '~',
            'https://grundfos.elearningcn.com/zh-CN/WeChat/s2019/externallogin',
          ) || defaultImage
      } else {
        teacherAvatarSrc.value = defaultImage
      }

      // 更新预约信息
      reservationCount.value = res.data.reservationCount
      // 用户类型为2表示已预约
      isReservation.value = res.data.myUserTypeToThislive === 2
    } else {
      uni.showToast({
        title: res.message || '获取直播详情失败',
        icon: 'none',
      })
    }
  } catch (error) {
    uni.hideLoading()
    uni.showToast({
      title: '获取直播详情失败',
      icon: 'none',
    })
    console.error('获取直播详情失败:', error)
  }
}

onMounted(() => {})

onLoad((options) => {
  // 从URL获取liveId参数
  if (options.liveId) {
    liveId.value = Number(options.liveId)
    fetchLiveDetail(liveId.value)
  } else {
    uni.showToast({
      title: '未找到直播信息',
      icon: 'none',
    })
  }
})
</script>

<style scoped lang="scss">
.live-reservation {
  width: 750rpx;
  background-color: #e6effc;
  position: relative;
  padding-bottom: 100rpx;
}

.back-btn {
  position: fixed;
  top: 100rpx;
  left: 30rpx;
  width: 80rpx;
  height: 80rpx;
  background-color: rgba(0, 0, 0, 0.53);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

.back-icon {
  width: 23rpx;
  height: 40rpx;
}

.banner-container {
  width: 750rpx;
  height: 500rpx;
  position: relative;
}

.banner-image {
  width: 750rpx;
  height: 500rpx;
}

.reservation-count {
  color: grey;
  font-size: 24rpx;
  padding: 6rpx 16rpx;
  right: 0;
  position: absolute;
  right: 0;
}

.live-info {
  width: 710rpx;
  margin: 20rpx auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.live-title {
  font-size: 32rpx;
}

.live-time {
  font-size: 25rpx;

  height: 60rpx;
  line-height: 60rpx;
}

.teacher-info {
  width: 710rpx;
  margin: 20rpx auto;
  background-color: #ffffff;
  border-radius: 12rpx;
  height: 180rpx;
  display: flex;
  align-items: center;
}

.teacher-avatar-container {
  width: 120rpx;
  height: 120rpx;
  margin-left: 30rpx;
}

.teacher-avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 60rpx;
  background-color: #f0f0f0;
}

.teacher-detail {
  width: 200rpx;
  height: 120rpx;
  margin-left: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border-right-color: grey;
  border-right-style: solid;
  border-right-width: thin;
}

.teacher-name {
  font-size: 30rpx;
  color: #333333;
  height: 80rpx;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
}

.teacher-tag {
  width: 120rpx;
  height: 40rpx;
  line-height: 40rpx;
  text-align: center;
  font-size: 24rpx;
  color: #ffffff;
  background-color: #1e5792;
  border-radius: 6rpx;
}

.teacher-field {
  flex: 1;
  height: 120rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-left: 20rpx;
}

.field-label {
  font-size: 28rpx;
  height: 50rpx;
  line-height: 50rpx;
  display: flex;
  align-items: center;
}

.field-value {
  font-size: 26rpx;
  color: #333333;
  height: 50rpx;
  line-height: 50rpx;
  margin-left: 40rpx;
}

.live-detail {
  width: 710rpx;
  margin: 20rpx auto;
  background-color: #ffffff;
  border-radius: 12rpx;
  min-height: 200rpx;
  padding: 20rpx;
  box-sizing: border-box;
}

.detail-title {
  font-size: 32rpx;
  height: 60rpx;
  line-height: 60rpx;
  margin-left: 25rpx;
}

.detail-content {
}

.reservation-btn {
  width: 700rpx;
  left: 30rpx;
  height: 80rpx;
  background-color: $uni-color-primary;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 28rpx;
  border-radius: 15rpx;
  margin-bottom: 100rpx;
  margin-left: 25rpx;
}
</style>
